package com.usian.controller;

import com.github.pagehelper.PageInfo;
import com.usian.pojo.*;
import com.usian.service.*;
import net.sf.jsqlparser.statement.select.Top;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Email 2781921355@qq.com
 * @Author 马忠豪
 * @Date 2023/8/23 18:50
 */
@RestController
@RequestMapping("zy")
public class ZyController {
    @Autowired
    private CompetitionService competitionService;
    @Autowired
    private TrainService trainService;
    @Autowired
    private CourseService courseService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private TopicService topicService;
    @Autowired
    private TagService tagService;
    @Autowired
    private CatalogService catalogService;
    @Autowired
    private PaperService paperService;
    @RequestMapping("findCompertition")
    public Integer findCompertition(HttpServletRequest request){
        String token = request.getHeader("token");
        return competitionService.findCompertition(token);
    }
    @RequestMapping("findTrain")
    public Integer findTrain(HttpServletRequest request){
        String token = request.getHeader("token");
        return trainService.findTrain(token);
    }
    @RequestMapping("findCourse")
    public Integer findCourse(HttpServletRequest request){
        String token = request.getHeader("token");
        return courseService.findCourse(token);
    }
    @RequestMapping("findCourseName")
    public List<Course> findCourseName(){
        return courseService.findCourseName();
    }
    @RequestMapping("toDianzan")
    public Integer toDianzan(Integer id,HttpServletRequest request){
        Integer o = (Integer) redisTemplate.opsForValue().get(id);
        if (o==null){
            redisTemplate.opsForValue().set(4,5);
            redisTemplate.opsForValue().set(1,18);
            redisTemplate.opsForValue().set(3,19);
            redisTemplate.opsForValue().set(6,25);
            redisTemplate.opsForValue().set(7,18);
            redisTemplate.opsForValue().set(8,18);
            redisTemplate.opsForValue().set(9,7);
            redisTemplate.opsForValue().set(10,25);
        }
        String token = request.getHeader("token");
       int i= courseService.toDianzan(id,token);
       return i;
    }
    @RequestMapping("getNewCourse")
    public PageInfo<Course> getNewCourse(@RequestBody CourseDto courseDto){

        return courseService.getNewCourse(courseDto);
    }
    @RequestMapping("getNewTrain")
    public PageInfo<Train> getNewTrain(@RequestBody TrainDto trainDto){
        return trainService.getNewTrain(trainDto);
    }
    @RequestMapping("findNewTrain")
    public List<Train> findNewTrain(){
        return trainService.findNewTrain();
    }
    @RequestMapping("findNewPaper")
    public List<Paper> findNewPaper(){
        return topicService.findNewPaper();
    }
    @RequestMapping("findTag")
    public List<Tag> findTag(){
        return tagService.findTag();
    }
    @RequestMapping("getInfo")
    public void getInfo(Integer id,Integer sum){
        redisTemplate.opsForValue().set("infoId",id);
        redisTemplate.opsForHash().put("学习",id,sum);

    }
    @RequestMapping("getNewCourseTo")
    public CourseVo getNewCourseTo(){
        return courseService.getNewCourseTo();
    }
    @RequestMapping("getcatalog")
    public List<CatalogVo> getcatalog(HttpServletRequest request){
        String token = request.getHeader("token");
    return  catalogService.getcatalog(token);
    }
    @RequestMapping("getCatalogSum")
    public Integer getCatalogSum(){
        Integer sum = (Integer) redisTemplate.opsForValue().get("章节数目");
        return sum;
    }
    @RequestMapping("addCatalogId")
    public Integer addCatalogId(Integer id,HttpServletRequest request){
        String token = request.getHeader("token");
       return catalogService.addCatalogId(id,token);
    }
    @RequestMapping("findProgress")
    public Integer findProgress(HttpServletRequest request){
        String token = request.getHeader("token");
     return catalogService.findProgress(token);
    }
    @RequestMapping("getCompertition")
    public PageInfo<CompetitionVo> getCompetition(@RequestBody CompetitionDto2 competitionDto2){
        return competitionService.getCompetition(competitionDto2);
    }
    @RequestMapping("getCInfo")
    public void  getCInfo(@RequestBody CompetitionVo competitionVo){
        redisTemplate.opsForValue().set("竞赛",competitionVo);
        redisTemplate.opsForValue().set("竞赛Id",competitionVo.getId());
    }
    @RequestMapping("getNewCompetition")
    public CompetitionVo getNewCompetition(){
        CompetitionVo vo = (CompetitionVo) redisTemplate.opsForValue().get("竞赛");
        return vo;
    }

    @RequestMapping("getTeamName")
    public List<Paper> getTeamName(){
        return paperService.getTeamName();
    }
    @RequestMapping("getTopicId")
    public List<Topic> getTopicId(Integer id, HttpServletRequest request, HttpSession session){
        String token = request.getHeader("token");
        Consumer o = (Consumer) redisTemplate.opsForValue().get(token);
        Integer consumerId = o.getId();
        List<Topic> list = topicService.getTopicId(id, consumerId);
        Integer num=0;
            for (Topic vo : list) {
                if (vo.getLogo2().equals("1")){
                    num+=vo.getSum();
                }
            }
        redisTemplate.opsForValue().set("分数12345",num);
        return list;
    }
    @RequestMapping("getName")
    public List<Option> getName(Integer id){
        return topicService.getName(id);
    }
    @RequestMapping("addTopicId")
    public void addTopicId(Integer id,String answer,Integer topicId,HttpServletRequest request){
        String token = request.getHeader("token");
        Consumer o = (Consumer) redisTemplate.opsForValue().get(token);
        Integer consumerId = o.getId();
        topicService.addTopicId(id,topicId,consumerId,answer);
    }
    @RequestMapping("getSelectAnswer")
    public String getSelectAnswer(Integer paperId,Integer topicId,HttpServletRequest request){
        String token = request.getHeader("token");
        Consumer o = (Consumer) redisTemplate.opsForValue().get(token);
        Integer consumerId = o.getId();
        return topicService.getSelectAnswer(consumerId,paperId,topicId);
    }
    @RequestMapping("getFenShu")
    public Integer getFenShu(){
        Integer num = (Integer) redisTemplate.opsForValue().get("分数12345");
        return num;
    }
    @RequestMapping("gettrainInfo")
    public void gettrainInfo(@RequestBody Train train){
        redisTemplate.opsForValue().set("培训ID",train.getId());
        redisTemplate.opsForValue().set("培训",train);
    }
    @RequestMapping("getTrainName")
    public Train getTrainName(){
        Train train  = (Train) redisTemplate.opsForValue().get("培训");
        return  train;
    }
    @RequestMapping("getTrainNum")
    public List<Integer> getTrainName(HttpServletRequest request){
        String token = request.getHeader("token");
        Integer trainId = (Integer) redisTemplate.opsForValue().get("培训ID");
        return trainService.getTrainNum(token,trainId);
    }
    @RequestMapping("addTrainConsumer")
    public void addTrainConsumer(HttpServletRequest request){
        String token = request.getHeader("token");
        Integer trainId = (Integer) redisTemplate.opsForValue().get("培训ID");
        trainService.addTrainConsumer(token,trainId);
    }
    @RequestMapping("getTrainCourse")
    public List<Course> getTrainCourse(HttpServletRequest request){
        String token = request.getHeader("token");
        return courseService.getTrainCourse(token);
    }
    @RequestMapping("getTrainPaper")
    public List<Paper> getTrainPaper(){
       return paperService.getTrainPaper();
    }
}
